package com.fansl.allround.auth.provider.ucenter.openid;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.Collection;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/10/16 13:53
 */
public class OpenIdAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
    private final Object principal;
    private final String providerId;
    private final String accessToken;

    public OpenIdAuthenticationToken(String openId, String providerId,
                                     String accessToken) {
        super(null);
        this.principal = openId;
        this.providerId = providerId;
        this.accessToken = accessToken;
        this.setAuthenticated(false);
    }

    public OpenIdAuthenticationToken(Object principal, String providerId,
                                     String accessToken,
                                     Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.providerId = providerId;
        this.accessToken = accessToken;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    public String getProviderId() {
        return providerId;
    }

    public String getAccessToken() {
        return accessToken;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }
}
